
//搜索用的数据
var REQUEST_DATA = {

    page : 1,

    per_page : 10,

    pay_status : ''

};

//查看调货列表时需要的数据
var WATCH_DATA = {

    page : 1,

    per_page : 10,

    sort_by : '',

    order : ''

};

//vue的data
var _Global_Data = {

    "provider_status": {

        "0": "客户自有商品",

        "1": "未申请调货",

        "5": "调货申请中",

        "10": "已支付",

        "20": "已发货",

        "30": "已收货"

    },                      //调货处理状态映射表

    "pay_status_map": {
        "0": "未支付",
        "1": "正在支付",
        "2": "支付成功",
        "5": "支付不成功",
        "10": "申请退款中",
        "20": "退款发放中",
        "30": "退款被拒绝",
        "40": "退款已完成"
    },                      //支付状态映射表

    process_status_map : {

        '-1' : '已取消',

        '0' : '待发货',

        '1' : '待收货',

        '2' : '待评价',

        '3' : '已评价',

        '5' : '处理中'

    },                      //货物状态映射表

    product_query_info : REQUEST_DATA,      //商品查询所需要的数据

    transfer_query_info : WATCH_DATA,      //调货查询所需要的数据

    product_search_info : {},       //用于接受商品查询数据

    transfer_search_info : {},       //用于接受调货查询数据

    checkall : false,           //是否选择所有

    transfer_detail : [],           //调货详情

    product_info : {},            //订单信息

    transfer_info : {},          //调货信息的订单信息

    address : '',                //客户填写的地址信息

    phone: '',                    //客户名称

    normal_sort : {

        paid_at : false,

        created_at : false,

        provider_status : false

    },                             //正序

    wx_code : '',               //微信二维码

    alipay_code : ''            //支付宝二维码

};

// 调货
var TransferResource = new Resource( API_LIST.TRANSFER_LIST , Vue.http );

//确认收货
var SignResource = new Resource( API_LIST.CONFIRM_SIGN , Vue.http );

!function transferCreate() {

    var vm = new Vue({

        el : '#transfer_list',

        data : _Global_Data,

        created : function(){

            this.productList();

            this.transferList();

        },

        computed : {


        },

        watch : {

            'checkall' : function checkall(){

                var self = this;

                this.product_info.data.forEach(function(product){

                    product.is_selected = self.checkall;

                })

            }

        },

        methods : {
            //排序
            sortFun : function sortFun(e){

                this.transfer_query_info.sort_by = e.target.dataset.name;

                if( this.normal_sort[e.target.dataset.name] ){

                    this.normal_sort[e.target.dataset.name] = !this.normal_sort[e.target.dataset.name];

                    this.transfer_query_info.order = 'desc';

                }else {

                    this.normal_sort[e.target.dataset.name] = !this.normal_sort[e.target.dataset.name];

                    this.transfer_query_info.order = 'asc';

                }

                this.transfer_search_info = JSON.parse(JSON.stringify(this.transfer_query_info));

                this.transferList(this.transfer_query_info);

            },
            //获取select2值
            getValue : function getValue(e){

                this.product_query_info.pay_status = e.target.value;

            },
            //商品列表改变页数的时候出发的函数
            productPageChange : function productPageChange(val){

                this.product_search_info.page = val;

                this.product_search_info.per_page = this.product_query_info.per_page;

                this.product_search_info.order_type = this.product_query_info.order_type;

                this.productList(this.product_search_info);

            },
            //商品列表改变每页显示数时触发的函数
            productSizeChange : function productSizeChange(val){

                this.product_search_info.per_page = val;

                this.product_query_info.per_page = val;

                this.productList(this.product_search_info);

            },
            //调货列表改变页数的时候出发的函数
            transferPageChange : function transferPageChange(val){

                this.transfer_search_info.page = val;

                this.transfer_search_info.per_page = this.transfer_query_info.per_page;

                this.transferList(this.transfer_search_info);

            },
            //调货列表改变每页显示数时触发的函数
            transferSizeChange : function transferSizeChange(val){

                this.transfer_search_info.per_page = val;

                this.transfer_query_info.per_page = val;

                this.transferList(this.transfer_search_info);

            },
            //点击搜索时触发的函数
            confirmSearch : function confirmSearch(){

                var json_object = JSON.stringify(this.product_query_info);

                this.product_search_info = JSON.parse(json_object);

                this.productList(this.product_query_info);

            },
            //确认收货
            confirmSign : function confirmSign(id){

                var self = this;

                var post_data = {

                    transfer_cargo_id : id

                };

                SignResource.store(post_data)
                            .then(function (res) {

                                if (parseInt(res.data.code) === 0) {

                                    self.transferList();

                                    alert('确认成功！');

                                } else {

                                    throw new Error(res.data.error);

                                }

                            })
                            .catch(function (err) {

                                console.log(err);

                                alert(err);

                            })

            },
            //点击跳转产品查看
            toProduct : function toProduct(){

                window.open(API_LIST.PRODUCT_DETAIL + '#tab_2');

            },
            //调货支付
            transferPay : function transferPay(id){

                this.alipay_code = location.origin + '/admin/transfer-cargo/ali-pay?transfer_cargo_id=' + id;

                this.wx_code = location.origin + '/admin/transfer-cargo/wx-pay?transfer_cargo_id=' + id;

            },
            //查看调货详情
            transferDetail : function transferDetail(id){

                var self = this;

                App.blockUI('#detail_form');

                TransferResource.show(id)
                                .then(function (res) {

                                    if (parseInt(res.data.code) === 0) {

                                        self.transfer_detail = res.data.transfer_cargo_list;

                                    } else {

                                        alert(res.data.error);

                                    }

                                    App.unblockUI('#detail_form');

                                })
                                .catch(function (err) {

                                    console.log(err);

                                    App.unblockUI('#detail_form');

                                    alert('网络异常！');

                                })
            },
            //查看订单详情
            orderDetail : function orderDetail(id){

                window.open( API_LIST.ORDER_URL + id );

            },
            //生成调货单
            createTransfer : function createTransfer(){

                var self = this;
                
                var selected_ids = [];

                this.product_info.data.forEach(function(data){

                    if( data.is_selected ){

                        selected_ids.push(data.id);
                        
                    }

                });

                if( selected_ids.length == 0 ){

                    alert('请至少选择一个需要调货的商品！');

                    return;

                }

                if( this.address == '' ){

                    alert('您还未填写地址！');

                    return;

                }

                App.blockUI('#address_form');

                var post_data = {

                    order_detail_ids : selected_ids,

                    customer_remark : this.address

                };

                TransferResource.store(post_data)
                                .then(function (res) {

                                    if (parseInt(res.data.code) === 0) {

                                        self.checkall = false;

                                        alert('创建成功！');

                                        self.address = '';

                                        self.transferList();

                                        self.productList();

                                    }else if( parseInt(res.data.code) === 1002 ){

                                        if( window.confirm('当前含有未接订单，是否确定接单并调货？') ){

                                            var force_create = {

                                                order_detail_ids : selected_ids,

                                                customer_remark : self.address,

                                                process_status : 5

                                            };

                                            TransferResource.store(force_create)
                                                            .then(function (res) {

                                                                if (parseInt(res.data.code) === 0) {

                                                                    self.checkall = false;

                                                                    alert('创建成功！');

                                                                    self.address = '';

                                                                    self.transferList();

                                                                    self.productList();

                                                                } else {

                                                                    alert(res.data.error);

                                                                }

                                                            })

                                        }else {



                                        }

                                    } else {

                                        alert(res.data.error);

                                    }

                                    $('.close').trigger('click');

                                    App.unblockUI('#address_form');

                                })
                                .catch(function (err) {

                                    console.log(err);

                                    App.unblockUI('#address_form');

                                    alert('添加失败！');

                                })

            },
            //获取所有符合调货条件的订单
            productList : function productList(params){

                var self = this;

                App.blockUI('#list_form');

                TransferResource.create(params)
                                .then(function (res) {

                                    if (parseInt(res.data.code) === 0) {

                                        res.data.product_list.data.forEach(function(product){

                                            product.is_selected = false;

                                        });

                                        self.product_info = res.data.product_list;

                                        self.product_info.total = parseInt(self.product_info.total);

                                        self.product_info.per_page = parseInt(self.product_info.per_page);

                                        self.product_info.current_page = parseInt(self.product_info.current_page);

                                    } else {

                                        throw new Error(res.data.error);

                                    }

                                    App.unblockUI('#list_form');

                                })
                                .catch(function (err) {

                                    console.log(err);

                                    App.unblockUI('#list_form');

                                    alert(err);

                                })

            },
            //获取所有调货列表
            transferList : function transferList(params){
                
                var self = this;

                App.blockUI('#list_form');

                TransferResource.index(params)
                                .then(function (res) {

                                    if (parseInt(res.data.code) === 0) {

                                        self.transfer_info = res.data.transfer_cargoes;

                                        self.transfer_info.total = parseInt(self.transfer_info.total);

                                        self.transfer_info.per_page = parseInt(self.transfer_info.per_page);

                                        self.transfer_info.current_page = parseInt(self.transfer_info.current_page);

                                    } else {

                                        throw new Error(res.data.error);

                                    }

                                    App.unblockUI('#list_form');

                                })
                                .catch(function (err) {

                                    console.log(err);

                                    App.unblockUI('#list_form');

                                    alert(err);

                                })
                
            }
        }

    })

}();